package edu.nju.mutest.visitor.collector.cond;

import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.types.ResolvedType;

import java.util.Set;

/**
 * A functional interface. To help to determine whether a node should be collected.
 * IMPORTANT: This Interface Has Been Changed!
 *
 * @author Adian
 */
public interface CollectionCond<T extends Expression> {

    /**
     * This method is mainly used in the default implement of method {@link CollectionCond#willCollect(Expression)}
     *
     * @return The names of valid resolved types as a Set of String. Example: "int"
     */
    Set<String> getValidResolvedTypes();

    /**
     * judge if an {@link Expression} should be collected.
     */
    default boolean willCollect(T target) {
        ResolvedType resolvedType = target instanceof MethodCallExpr ?
                ((MethodCallExpr) target).resolve().getReturnType() : target.calculateResolvedType();

        return getValidResolvedTypes().contains(resolvedType.describe());
    }
}
